// pages/draftFkDetails/draftFkDetails.js
import api from "../../utils/api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    draftID:'',
    uuid: '',
    detailList: '',
    passed_active: 0,
    // fkDraftClaimList: [], 
    editable: '', // 是否可以编辑（只有申请人可以）
    flowList: [], // 审批流程
    zanwu: false, // 暂无审批流程
    isExaminer: '', // 审批意见显示与否
    //审批
    fixed: true,
    message: '',
    shenpishow: false,
    flowId: '', // 流程ID
  },
    //汇票确认 （状态 已确认 未确认）
    hpqueren(e){
      console.log(e,"点击确认")
        let that = this
        api.hpqueren({
          data:{
            id: e.currentTarget.dataset.id,
          },
          success:function(res){
            console.log(res.data.data, "确认 okkk")
            if (res.data.code == 200) {
              that.getDate()
            }
          }
        })
      },
     //汇票取消
     hpqixiao(e){
      console.log(e,"点击取消")
        let that = this
        api.hpquxioa({
          data:{
            id: e.currentTarget.dataset.id,
          },
          success:function(res){
            console.log(res.data.data, "取消 okkk")
            if (res.data.code == 200) {
              that.getDate()
            }
          }
        })
     },
//切换选项卡
onChange(event) {
  // console.log(event.detail);
  if (event.detail.index == 1) {
    this.invProcess()
  } else {
    this.voidinvProcess()
  }
},
  //获取详情数据
  getDate() {
    let that = this
    api.draftgetFkDetail({
      data: {
        id: this.data.draftID
      },
      success: function (res) {
        console.log(res.data.data, "汇票 详情")
        if (res.data.code == 200) {
          if (res.data.data.fkDraftClaimList.length > 0) {
            for (let i = 0; i < res.data.data.fkDraftClaimList.length; i++) {
              res.data.data.fkDraftClaimList[i].money = that.addComma(res.data.data.fkDraftClaimList[i].money)
              res.data.data.scMoney = that.addComma(res.data.data.scMoney)
              res.data.data.ycMoney = that.addComma(res.data.data.ycMoney)
              res.data.data.totalMoney = that.addComma(res.data.data.totalMoney)
              res.data.data.money =  that.addComma(res.data.data.money)
            }
          }
          that.setData({
            detailList: res.data.data,
            userDept: res.data.data.userDept,
            uuid:res.data.data.uuid,
            fkDraftClaimList:res.data.data.fkDraftClaimList,
            remarks: res.data.data.remarks,
            dfdwmc: res.data.data.dfdwmc,
            isExaminer: res.data.data.isExaminer,//审批意见显示与否
            editable:res.data.data.editable,//底部按钮是否可编辑
          })
        }
      }
    })
  },
  //编辑
  edit() {
      wx.navigateTo({
        url: '/pages/editdraft/editdraft?id='+this.data.draftID
      })
  },
  //审批流程
  invProcess() {
    let that = this;
    api.draftProcess({
      data: {
        uuid: this.data.uuid
      },
      success: function (res) {
        console.log(res.data.data, '汇票详情 -- 审批流程')
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].flowStepStatus == null) {
              that.data.flowId = res.data.data[i].flowId
            }
          }
          if (res.data.data.length > 0) {
            that.setData({
              flowList: res.data.data,
              flowId: that.data.flowId
            })
          } else {
            that.setData({
              zanwu: true
            })
          }

        }
      }
    })
  },
  //输入审批意见
  onshenpiInput() {
    this.setData({
      shenpishow: true
    })
  },
  //关闭弹窗
  onshenpiClose() {
    this.setData({
      shenpishow: false
    })
  },
  //输入审批意见
  onshenpiChange(e) {
    console.log(e.detail,'审批意见')
    this.setData({
      message: e.detail
    })
  },
  // 拒绝
  onDecline() {
    let that = this;
    api.draftProcessJujue({
      data: {
        fkId: that.data.draftID,
        flowId: that.data.flowId,
        flowNotes: that.data.message,
      },
      success: function (res) {
        console.log(res,'拒绝')
        if (res.data.code == 200) {
          wx.showToast({
            title: '审批成功',
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 100);
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
      }
    })
  },
  // 同意
  onAgree() {
    let that = this;
    api.draftProcessTongyi({
      data: {
        fkId: that.data.draftID,
        flowId: that.data.flowId,
        flowNotes: that.data.message,
      },
      success: function (res) {
        console.log(res,'同意')
        if (res.data.code == 200) {
          wx.showToast({
            title: '审批成功',
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 100);
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
      }
    })
  },
  formatDate(date) {
    //转时间戳
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  addComma(date) {
    let str = '';
    let num = '';
    let newStr = '';
    date = date + '';
    if (date.indexOf('.') != -1) {
      num = date.split('.')[0]
      str = date.split('.')[1]
    } else {
      num = date
    }
    newStr = num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
    newStr = str == '' ? newStr : newStr + '.' + str;
    return newStr;
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      draftID: options.id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.data.unitId = wx.getStorageSync('userInfo').unitId
    this.data.depId = wx.getStorageSync('userInfo').depId, //登录人的部门id
    this.getDate()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})